package reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test5 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class c = Class.forName("reflection.User");
        System.out.println(c.getClassLoader());
        //获取类的名字
        System.out.println(c.getName());
        System.out.println(c.getSimpleName());
        //获取类的属性
        System.out.println("==========获取类的属性========");
        Field[] fields = c.getFields();//公开属性
        for (Field field : fields) {
            System.out.println(field);
        }
        System.out.println("----------------");
        fields=c.getDeclaredFields();//所有属性
        for (Field field : fields) {
            System.out.println(field);
        }
        //通过名称获取指定的属性
        Field age = c.getDeclaredField("age");
        System.out.println(age);

        //获得类的方法
        System.out.println("==========获取类的方法========");
        Method[] methods = c.getMethods();//所有公共方法，包括父类的方法
        for (Method method : methods) {
            System.out.println("公开的方法："+method);
        }
        System.out.println("----------------");
        methods=c.getDeclaredMethods();//本类所有方法
        for (Method method : methods) {
            System.out.println("所有的方法:"+method);
        }
        System.out.println(c.getDeclaredMethod("a"));
        System.out.println(c.getMethod("setAge", int.class));

        System.out.println("==========获取类的构造方法========");
        Constructor[] constructors = c.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
        constructors=c.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
            System.out.println("#"+constructor);
        }
        System.out.println("指定的构造器"+c.getConstructor(String.class, int.class));
    }
}
